package org.yxjme.yxj_ijkplayerlib;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;

public class SDTextView extends android.support.v7.widget.AppCompatTextView {


    private String TAG = "[SDTextView]" ;
    /**
     *
     */
    private int mViewWidth ;

    /**
     *
     */
    private LinearGradient mLinearGradient;

    /**
     * 画笔
     *
     */
    private Paint paint ;


    /**
     *
     */
    private Matrix mMatrix ;


    public SDTextView(Context context) {
        this(context,null);
    }

    public SDTextView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SDTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.v(TAG,"onMeasure");
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.v(TAG,"onSizeChanged");
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.v(TAG,"onLayout");
        if (mViewWidth == 0){

            mViewWidth = getMeasuredWidth() ;

            paint = getPaint() ;


            mLinearGradient = new LinearGradient(
                    0,0,
                    mViewWidth,0,
                    new int[]{Color.RED,Color.BLUE,Color.YELLOW},
                    null,
                    Shader.TileMode.CLAMP);

            paint.setShader(mLinearGradient) ;

            mMatrix = new Matrix() ;
        }
    }



    int mTempTranX ;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.v(TAG,"onDraw");
        if (mMatrix!=null){
            mTempTranX += mViewWidth / 5 ;
            if (mTempTranX>2*mViewWidth){
                mTempTranX = -mViewWidth ;
            }
            mMatrix.setTranslate(mTempTranX,0);
            mLinearGradient.setLocalMatrix(mMatrix);
            postInvalidateDelayed(100);
        }
    }
}
